#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"

/********************************************************************************************
VT5100c 6088 Dante
********************************************************************************************
********************************************************************************************/

void VT5100_Enable_Flash_Control_and_SPI_Flash_En(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u16 tmp=0;
	u8 buffer1[5];	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);	
	delay_ms(15);
	buffer1[0] = 0x01;
	buffer1[1] = 0x28;                    
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	buffer1[0] = 0x03;
	buffer1[1] = 0xF0; 
	buffer1[2] = 0xAA; 
	buffer1[3] = 0x10; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	buffer1[0] = 0x03;
	buffer1[1] = 0xEA; 
	buffer1[2] = 0x30; 
	buffer1[3] = 0x00; 
	SSD2828_W_Array(SigMode,channel,buffer1,0); 
	buffer1[0] = 0x02;
	buffer1[1] = 0xEE; 
	buffer1[2] = 0xAB; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	buffer1[0] = 0x02;
	buffer1[1] = 0xED; 
	buffer1[2] = 0x20; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);	
	delay_ms(100);  	
	buffer1[0] = 0x02;
	buffer1[1] = 0xED; 
	buffer1[2] = 0x00; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);		
	buffer1[0] = 0x02;
	buffer1[1] = 0xEE; 
	buffer1[2] = 0x06; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);		
	buffer1[0] = 0x02;
	buffer1[1] = 0xED; 
	buffer1[2] = 0x20; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(100); 			
	buffer1[0] = 0x02;
	buffer1[1] = 0xED; 
	buffer1[2] = 0x00; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);		
	buffer1[0] = 0x02;
	buffer1[1] = 0xEE; 
	buffer1[2] = 0x05; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);		
	buffer1[0] = 0x02;
	buffer1[1] = 0xED; 
	buffer1[2] = 0x08; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);	
	delay_ms(100); 
	buffer1[0] = 0x02;
	buffer1[1] = 0xED; 
	buffer1[2] = 0x00; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);	
	delay_ms(100); 
	//READ flash ID
	buffer1[0] = 0x02;
	buffer1[1] = 0xEE; 
	buffer1[2] = 0x9F; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);	
	buffer1[0] = 0x02;
	buffer1[1] = 0xED; 
	buffer1[2] = 0x44; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);	
	delay_ms(100); 	
	buffer1[0] = 0x02;
	buffer1[1] = 0xED; 
	buffer1[2] = 0x00; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	/***read***/
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
	SSD2828_W_Reg(SigMode,channel,0xC1,0x0003); //return package size                                
	SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
	buffer1[0] = 0x01;
	buffer1[1] = 0xEC;                        
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);  
	for(int i =0;i<1;i++)
	{
		 tmp=SSD2828_R_Reg(SigMode,channel,0xFF);  //SPI3_Read_u16_Data(channel);
		 buffer[4+i]=tmp;
		 buffer[5+i]=tmp>>8;
		 delay_ms(2);
	}
	buffer[0] = 0x2F;
	buffer[1] = 0x06;	
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}
void VT5100_Inter_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[3];
	VTDR6100_Step_Five(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
	buffer[0] = 0x2F;
	buffer[1] = 0x01;
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);        																					
}
                                    
void VT5100_Exit_Demura_Ram(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}
                               
void VT5100_Flash_Write_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		VTDR6100_OTP_Flash(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		
		buffer[0] = 0x2F;
		buffer[1] = 0x03;		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);		
}                       

void VT5100_Flash_Write_Demura2(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}


void VT5100_Flash_Check_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u16 tmp=0;
	u8 buffer1[5];
	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	  delay_ms(5);

	  buffer1[0] = 0x03;
    buffer1[1] = 0xFF;
	  buffer1[2] = 0x5A;
	  buffer1[3] = 0x80;
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,0x0009); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer1[0] = 0x01;
    buffer1[1] = 0xfa;                        
    SSD2828_W_Array(SigMode,channel,buffer1,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
		 for(i=0;i<9;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(2);
        i++;
    }
	
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x04;					
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  	
}
                                  
void VT5100_Flash_Erase_Demura(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		u16 tmp;
		u8 buffer1[5];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
	
	VTDR6100_Erase_Flash(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);	
	delay_ms(5);
	VTDR6100_Flash_Statu(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
	delay_ms(5);
	VTDR6100_Check_Flash(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
	delay_ms(5);
	
	buffer[0] = 0x2F;
	buffer[1] = 0x05;		
	buffer[4] = 0x00;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú	
}

void VT5100_Flash_Demura_OTP(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}

void VT5100_Demura_Function_ON(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	

	buffer1[0] = 0x02;
	buffer1[1] = 0x59;
	buffer1[2] = 0x09;
	SSD2828_W_Array(SigMode,channel,buffer1,0);	
	
	buffer[0] = 0x2F;
	buffer[1] = 0x07;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú
}

void VT5100_Demura_Function_OFF(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);

	buffer1[0] = 0x02;
	buffer1[1] = 0x59;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);	
	
	delay_ms(5);
	buffer[0] = 0x2F;  //Demura_type
	buffer[1] = 0x08;				
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}

void ProcessForDmuR08( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      //写寄存器
					VT5100_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      //读寄存器
					VT5100_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      //读寄存器
					VT5100_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      //读寄存器
					VT5100_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      //读寄存器
					VT5100_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      //check flash ID
					VT5100_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      //check flash ID
					VT5100_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      //check flash ID
					VT5100_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			case 0x0A:
					VT5100_Flash_Write_Demura2(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			default:
				break;												
	}
}
//********************************************************************************************